package cjq.demo.thread.ch4_11;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/5/1.
 */
public class ReportProcessor implements Runnable {

    private CompletionService<String> service;

    private boolean end;

    public ReportProcessor(CompletionService<String> service){
        this.service = service;
        end = false;
    }

    public void run(){
        while(!end){
            try{
                Future<String> result = service.poll(20, TimeUnit.SECONDS.SECONDS);
                if(result!=null){
                    String report = result.get();
                    System.out.println("ReportReceiver: Report Received: "+ report);
                }
            }catch(InterruptedException | ExecutionException e){
                e.printStackTrace();
            }

        }

        System.out.println("ReportSender : End");
    }

    public void setEnd(boolean end){
        this.end = end;
    }
}
